package guowei.gulistorage.storage.controller;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import guowei.common.exception.BizCodeEnume;
import guowei.common.to.es.WareSkuIdStocks;
import guowei.gulistorage.storage.exception.WareNotEnoughException;
import guowei.gulistorage.storage.feign.UserFeign;
import guowei.gulistorage.storage.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import guowei.gulistorage.storage.entity.WareSkuEntity;
import guowei.gulistorage.storage.service.WareSkuService;
import guowei.common.utils.PageUtils;
import guowei.common.utils.R;



/**
 * 商品库存
 *
 * @author guoguo
 * @email 1573746397@qq.com
 * @date 2023-05-21 12:24:20
 */
@RestController
@RequestMapping("storage/waresku")
public class WareSkuController {
    @Autowired
    private WareSkuService wareSkuService;

    /**
     * 锁定库存     用户当前订单上的商品都需要锁定，若库存不足等异常导致报错，则会回滚，回滚后当前订单不会保存数据库
     * @param lockVo
     * @return
     */
    @PostMapping("/lock/order/orderitems")
    public R lockItemStorage(@RequestBody OrderStorageLockVo lockVo){
        Boolean result = null;
        try {
            result = wareSkuService.tryLockItems(lockVo);
            return R.ok();
        } catch (WareNotEnoughException e) {
            return R.error(BizCodeEnume.WARE_NOT_ENOUGH.getCode(), BizCodeEnume.WARE_NOT_ENOUGH.getMsg());
        }
    }

    /**
     * 前端页面发送请求得到运费信息
     * @param addrId
     * @return
     */
    @GetMapping("/fare")
    public R getFareByAddrId(@RequestParam("addrId") Long addrId){
//得到用户详细信息
        AddressAndFareVo addressAndFareVo = wareSkuService.getfareId(addrId);
        return R.ok().put("data",addressAndFareVo);
    }

    /**
     * order服务远程调用该方法得到库存信息
     * @param ids
     * @return
     */
    @PostMapping("/ware/ids")
    public R getInfoBySkuId(@RequestBody List<Long> ids){
        List<StockHasVo> list = wareSkuService.getInfoBuSkuIds(ids);
        return R.ok().put("vos",list);
    }

    /**
     * 按照skuId查询各个存储点库存
     */
    @RequestMapping("/getstocks")
    public List<WareSkuIdStocks> getStocks(@RequestBody List<Long> skuIds){
        List<WareSkuIdStocks> list = wareSkuService.getAllStorages(skuIds);
        return list;
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("storage:waresku:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = wareSkuService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("storage:waresku:info")
    public R info(@PathVariable("id") Long id){
		WareSkuEntity wareSku = wareSkuService.getById(id);

        return R.ok().put("wareSku", wareSku);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("storage:waresku:save")
    public R save(@RequestBody WareSkuEntity wareSku){
		wareSkuService.save(wareSku);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("storage:waresku:update")
    public R update(@RequestBody WareSkuEntity wareSku){
		wareSkuService.updateById(wareSku);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("storage:waresku:delete")
    public R delete(@RequestBody Long[] ids){
		wareSkuService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
